package com.zjw.mycoolweather.util;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.zjw.mycoolweather.db.City;
import com.zjw.mycoolweather.db.County;
import com.zjw.mycoolweather.db.Province;
import com.zjw.mycoolweather.db.Weather;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by hp on 2017/8/10.
 */

public class Utility {

    /**
     * 解析和处理服务器返回的省级数据
     */
    public static boolean handleProvinceResponse(String response) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvinces = new JSONArray(response);
                for (int i = 0; i < allProvinces.length(); i++) {
                    JSONObject provinceObject = allProvinces.getJSONObject(i);
                    String provinceName = provinceObject.getString("name");
                    int provinceCode=provinceObject.getInt("id");
                    Province province=new Province();
                    province.setProvinceName(provinceName);
                    province.setProvinceCode(provinceCode);
                    province.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 解析和处理服务器返回的市级数据
     */
    public static boolean handleCityResponse(String response,int provinceId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCities = new JSONArray(response);
                for (int i = 0; i < allCities.length(); i++) {
                    JSONObject cityObject = allCities.getJSONObject(i);
                    String cityName = cityObject.getString("name");
                    int cityCode=cityObject.getInt("id");
                    City city=new City();
                    city.setCityName(cityName);
                    city.setCityCode(cityCode);
                    city.setProvinceId(provinceId);
                    city.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 解析和处理服务器返回的县级数据
     */
    public static boolean handleCountyResponse(String response,int cityId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCounties = new JSONArray(response);
                for (int i = 0; i < allCounties.length(); i++) {
                    JSONObject countyObject = allCounties.getJSONObject(i);
                    String countyName = countyObject.getString("name");
                    String weatherId=countyObject.getString("weather_id");
                    County county=new County();
                    county.setCountyName(countyName);
                    county.setWeatherId(weatherId);
                    county.setCityId(cityId);
                    county.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 将返回的JSO你数据解析成Weather实体
     */
    public static Weather handleWeatherResponse(String response) {
        if (!TextUtils.isEmpty(response)) {
            try {
                //Log.d("zjw", "handleWeatherResponse: "+response);
                Weather weather = new Gson().fromJson(response, Weather.class);
                return weather;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
